package cs2340.spring13.team3.wheresmystuff.model;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.http.client.ClientProtocolException;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.annotate.JsonProperty;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.annotate.JsonSerialize;

import com.loiane.util.JsonDateSerializer;

/**
 * @author Tim Swihart <email obscured since this file is public>
 * @version 0.9
 * @since 2013-04-02
 */
public class Item extends ServerObject {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static final String DEFAULT_MATCH = "";
	protected static String BASE_URL = "/items/";

	private String name;

	private double reward;
	private int id;

	private List<Tag> tags;

	/**
	 * Whether the item is `lost` or `found`
	 */
	private Boolean found;

	@JsonProperty("archived")
	private Boolean archived;

	@JsonProperty("description")
	private String description;

	private Date date;

	private String matchName;

	// private String authId;

	public Item() {
		this.setName("");
		this.setTags(new ArrayList<Tag>());
	}

	public Item(String name, String description, Double reward, Boolean found,
			List<Tag> tags, Date d) throws ClientProtocolException, IOException {
		this.setName(name);
		this.setDescription(description);
		this.setReward(reward);
		this.setFound(found);
		this.setArchived(false);
		this.setDate(d); // TODO: get the date set properly. until then, use
							// current date to avoid errors
		this.setMatchName(DEFAULT_MATCH);
		if (tags != null) {
			this.setTags(tags);
		} else {
			this.setTags(new ArrayList<Tag>());
		}
		if (ServerObject.getObjectMapper() == null)
			ServerObject.om = new ObjectMapper();
		try {
			String json = ServerObject.getObjectMapper().writeValueAsString(
					this.toJson());
			create(json);
		} catch (SyncException se) {
			se.printStackTrace();
		}
	}

	public Item(JsonNode usr) {
		if (ServerObject.om == null)
			ServerObject.om = new ObjectMapper();
		try {
			Item u = om.treeToValue(usr, Item.class);
			this.setName(u.getName());
			this.setTags(u.getTags());
			this.setDescription(u.getDescription());
			this.setReward(u.getReward());
			this.setId(u.getId());
			this.setFound(u.getFound());
			this.setArchived(u.getArchived());
			this.setDate(u.getDate());
			this.setMatchName(DEFAULT_MATCH);
		} catch (JsonParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public Item(String json) {
		if (ServerObject.om == null)
			ServerObject.om = new ObjectMapper();
		try {
			Item tempItem = om.readValue(json, Item.class);
			this.setName(tempItem.getName());
			this.setReward(tempItem.getReward());
			this.setArchived(false);
			this.setFound(tempItem.getFound());
			this.setDescription(tempItem.getDescription());
			this.setTags(tempItem.getTags());
			this.setDate(tempItem.getDate());
			this.setId(tempItem.getId());
			this.setMatchName(DEFAULT_MATCH);
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void addTag(Tag t) {
		if (this.tags == null) {
			this.tags = new ArrayList<Tag>();
		}
		if (!this.tags.contains(t)) {
			this.tags.add(t);
		}
	}

	// Update and management methods

	public void removeTag(Tag t) {
		this.tags.remove(t);
	}

	@JsonProperty("name")
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@JsonProperty("reward")
	public double getReward() {
		return reward;
	}

	public void setReward(double reward) {
		this.reward = reward;
	}

	@JsonProperty("tags")
	public List<Tag> getTags() {
		return tags;
	}

	public void setTags(List<Tag> tags) {
		this.tags = tags;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public JsonNode toJson() {
		return om.valueToTree(this);
	}

	@JsonProperty("found")
	public Boolean getFound() {
		return found;
	}

	public void setFound(Boolean found) {
		this.found = found;
	}

	public Boolean getArchived() {
		return archived;
	}

	public void setArchived(Boolean archived) {
		this.archived = archived;
	}

	@Override
	public String getBaseURL() {
		return BASE_URL;
	}

	@JsonProperty("date")
	@JsonSerialize(using = JsonDateSerializer.class)
	public Date getDate() {
		if (date == null) {
			date = new Date();
		}
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	@Override
	public String toString() {
		return this.getName() + " ("
				+ JsonDateSerializer.DATE_FORMAT.format(this.getDate())
				+ ")";
	}

	/**
	 * @return the matchName
	 */
	@JsonProperty("match_name")
	public String getMatchName() {
		return matchName;
	}

	/**
	 * @param matchName
	 *            the matchName to set
	 */
	@JsonProperty("match_name")
	public void setMatchName(String matchName) {
		this.matchName = matchName;
	}

	@Override
	@JsonProperty("id")
	public void setId(int id) {
		this.id = id;
	}

	@Override
	@JsonProperty("id")
	public int getId() {
		return this.id;
	}
}